package com.gopay.scheduler.job;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.domain.User4Amount;
import com.gopay.common.user.dao.UserDailyBalStatsDAO;
import com.gopay.common.user.manager.UserDailyBalStatsManager;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
/**日终余额统计
 * @DailyBalanceStatisticsJob.java
 * @author 曹永峰
 *
 */
public class DailyBalanceStatisticsJob extends GopayBaseJob{
	
	private UserDailyBalStatsDAO userDailyBalStatsDao;
	private UserDailyBalStatsManager userDailyBalStatsManager;

	private Logger logger = LoggerFactory
			.getLogger(DailyBalanceStatisticsJob.class);
	
	public void doJobInternal(){
		logger.info("day end balance statistics...");
		String today = DateUtils.now2string("yy/MM/dd");
		
		int count =userDailyBalStatsDao.getCount();
		if(count==0){
			return;
		}
		//循环次数
		int i = (count+999)/1000;
		for (int j = 0; j < i; j++) {
			//获取日终余额的统计信息,每次1000条
			List<User4Amount> balanceInfo= userDailyBalStatsDao.getBalInfo(today,j*1000+1,1000);
			//增加统计信息
			saveBalanceInfo(balanceInfo);
		}
		
		logger.info("day end balance statistics end...");
	}
	private void saveBalanceInfo(List<User4Amount> balanceInfo){
		try{
			logger.info("increase statistical data in the database...");
			userDailyBalStatsManager.saveBalInfo(balanceInfo);
		}catch (Exception e) {
			logger.info("Increase the failure statistics....");
			e.printStackTrace();
		}
	}
	@Resource(name="userDailyBalStatsDao")
	public void setUserDailyBalStatsDao(UserDailyBalStatsDAO userDailyBalStatsDao) {
		this.userDailyBalStatsDao = userDailyBalStatsDao;
	}
	
	@Resource(name="userDailyBalStatsManager")
	public void setUserDailyBalStatsManager(
			UserDailyBalStatsManager userDailyBalStatsManager) {
		this.userDailyBalStatsManager = userDailyBalStatsManager;
	}
}
